<?php

namespace addons\workteam\controller;

use think\Config;
use think\Db;
use think\Queue;

/**
 * 定时任务
 */
class Autotask extends \think\addons\Controller
{
    protected $noNeedLogin = ["*"];
    protected $layout = '';

    public function _initialize()
    {
        set_time_limit(0);
        parent::_initialize();

        if (!$this->request->isCli()) {
           $this->error('只允许在终端进行操作!');
        }
    }

    /**
     * 汇总
     */
    public function index()
    {
        $this->expiretask();
        $this->expireproject();

    }

    /**
     * 工作统计
     */
    public function statistics()
    {
        set_time_limit(0);
        $adminModel = new \app\admin\model\Admin();
        $lists = $adminModel->select();
        $statement = new \app\admin\model\workteam\Statement();
        foreach ($lists as $row) {
            $statement->statistics(date('Y'), date('m', strtotime('-1 month')), $row->id);
        }
        //这里编写我们的定时任务逻辑
//        echo "done".PHP_EOL;
        return;
    }


    /**
     * 过期任务提醒
     */
    public function expiretask($offset = 0)
    {
        $limit = 1000;
        $thisModel = model('\app\admin\model\workteam\Task');

		$where = ['t.end_time' => ['<', time()], 't.status' => ['not in', '-1,2'], 't.admin_id' => ['<>', '0']];
        $lists_row = $thisModel->alias("t")->join("workteam_project p",'p.id =t.project_id')
			->where($where)->field('t.*,p.`name` as project_name,p.dd_webhook,p.wx_webhook')
			->fetchSql(false)->order('id asc')->limit($offset, $limit)->select();

        if ($lists_row) {
            foreach ($lists_row as $row) {
                $row['admin_ids'] = $row->admin_id.','.$row->admin_ids;
				$row['link'] = config('admin.crm_url').url('workteam/task/edit',['ids'=>$row->id]);
                $row['key'] = 'notice_expire_task';//逾期通知模板
                //通知队列
                Queue::push('addons\workteam\library\notice\queue\TaskJob', $row);
            }
        }

        if ($lists_row) {
            $offset = $offset + $limit;
            echo " expiretask".$offset.PHP_EOL;
            return $this->expiretask($offset);
        }else{
//            echo "done".PHP_EOL;
        }

        return;
    }

    /**
     * 过期项目提醒
     */
    public function expireproject($offset = 0)
    {
        $limit = 1000;
        $thisModel = model('\app\admin\model\workteam\Project');

		$where = ['end_time' => ['<', time()], 'project_admin_id' => ['<>','0'], 'status' => ['not in','-1,2,3']];
        $lists_row = $thisModel->where($where)->fetchSql(false)->order('id asc')->limit($offset, $limit)->select();

        if ($lists_row) {
            foreach ($lists_row as $row) {
                $row['admin_ids'] = $row->project_admin_id.','.$row->admin_ids;
				$row['link'] = config('admin.crm_url').url('workteam/project/detail',['ids'=>$row->id]);
                $row['key'] = 'notice_expire_project';//逾期通知模板
                //通知队列
                Queue::push('addons\workteam\library\notice\queue\ProjectJob', $row);
            }
        }

        if ($lists_row) {
            $offset = $offset + $limit;
            echo " expireproject".$offset.PHP_EOL;
            return $this->expireproject($offset);
        }else{
//            echo "done".PHP_EOL;
        }

        return;
    }


}